<template>
  <div>
    <el-steps :active="active" finish-status="success">
      <el-step title="账户注册"></el-step>
      <el-step title="信息填写"></el-step>
      <el-step title="账户激活"></el-step>
    </el-steps>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="头像" prop="headImage">
        <el-upload
            class="avatar-uploader"
            action="http://localhost:8080/user/upload/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="ruleForm.nickname"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="ruleForm.sex" placeholder="请选择性别">
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出生日期" required>
        <el-col :span="11">
          <el-form-item prop="birthday">
            <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.birthday" style="width: 100%;"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="邮箱" prop="emial">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="tel">
        <el-input v-model="ruleForm.tel"></el-input>
      </el-form-item>
      <el-form-item label="兴趣" prop="hobby">
        <el-input v-model="ruleForm.hobby"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="next">下一步</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
export default {
  name: "RegisterInfo.vue",
  data() {
    return {
      active: 1,
      imageUrl: '',
      ruleForm: {
        id:'',
        headImage: '',
        nickname: '',
        sex: '',
        birthday: '',
        email: '',
        tel: '',
        hobby: ''
      },
      rules: {
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'blur' },
          { min: 2, max: 18, message: '长度在 2 到 18 个字符', trigger: 'blur' }
        ],
        region: [
          { required: true, message: '请选择活动区域', trigger: 'change' }
        ],
        birthday: [
          { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
        ],
        hobby: [
          {required: true, message: '请至少填写一个兴趣爱好', trigger: 'change' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        tel: [
          { required: true, message: '请填写电话号码', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {

    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleAvatarSuccess(res, file) {
      debugger;
      console.log(res)
      console.log(file)
      this.imageUrl = URL.createObjectURL(file.raw);
      this.ruleForm.headImage=res.data;
    },
    beforeAvatarUpload(file) {
       const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

           if (!isJPG) {
             this.$message.error('上传头像图片只能是 JPG 格式!');
           }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return  isJPG&&isLt2M;
    },
    next() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          const _this = this
          debugger;
          this.$axios.post("/user/registerInfo", this.ruleForm).then(res => {
            if (res.data.code === 200) {
              this.$message({
                showClose: true,
                message: '信息填写成功',
                type: 'success'
              });
              _this.$router.push("/activeAccount/"+this.ruleForm.email+"/"+this.ruleForm.id)
            } else {
              this.$message.error(res.data.message);
              return false;
            }

          });
        }
      });

    }
  },
  created() {
    const userId = this.$route.params.userId
    this.id=userId
    const _this = this
    if(userId) {
      this.$axios.get('/user/getUserById/' + userId).then(res => {
        const User = res.data.data
        _this.ruleForm.id=User.id
        _this.ruleForm.nickname=User.nickname
        _this.ruleForm.sex=User.sex
        _this.ruleForm.birthday=User.birthday
        _this.ruleForm.headImage=User.headImage
        _this.ruleForm.email=User.email
        _this.ruleForm.tel=User.tel
        _this.ruleForm.hobby=User.hobby
      })
    }
  }

}
</script>
<style>
avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>